/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jp.co.irep.juten.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJB;
import javax.inject.Inject;
import javax.inject.Named;
import jp.co.irep.juten.entity.Place;
import jp.co.irep.juten.facade.PlaceFacade;
import jp.co.irep.juten.utils.BeanUtil;

import jp.co.irep.juten.view.PlaceViewBean;

/**
 *
 * @author toan_nguyen
 */
@Named
public class PlaceControllerBean {

    @EJB
    PlaceFacade placeFacade;

    @Inject
    private PlaceViewBean placeViewBean;

    public String search() {
        Map<String, Object> placeMap = new HashMap<>();
        placeMap.put("placeName", placeViewBean.getSearchPlaceName().trim());
        placeMap.put("prefectureId", placeViewBean.getSearchPrefectureId());
        placeViewBean.setPlaceList(placeFacade.selectPlaceByHashMap(placeMap));

        return "index";
    }

    public void resetSearchForm() {
        placeViewBean.setSearchPlaceName(null);
        placeViewBean.setSearchPrefectureId(0);
    }

//    public List getListProductOfPlace(Place place) {
//        placeService.getPlaceProductList(place.getId());
//    }
    /**
     * 編集ページに遷移する
     *
     * @return
     */
    public String gotoEditPage() {
        this.fillAttributesFromPlace(placeViewBean.getSelectedPlace());
//        placeViewBean.setPlaceProductList(placeService.getPlaceProductList(placeViewBean.getSelectedPlace().getId()));
//        
//        if(placeViewBean.getPlaceProductList()==null)
//            {
//    		BeanUtil.setMessage("error", BeanUtil.getMessageString("placeGotoEditPageError"));
//    		return null;
//            }
//        
//        placeViewBean.setProductListIdString("");
//        placeViewBean.setProductListNameString("");
////        List<PlaceProduct> list = placeViewBean.getPlaceProductList();
//        for (PlaceProduct placeProduct : placeViewBean.getPlaceProductList()) {
//                String productId = String.valueOf(placeProduct.getId());
//                placeViewBean.setProductListIdString(placeViewBean.getProductListIdString()+productId +",");
//                placeViewBean.setProductListNameString(placeViewBean.getProductListNameString()+"<span id = \"span-product-" + productId  + "\"><br><a href=\"javascript:deleteProduct(" +productId + ");\">削除</a>" + placeProduct.getProduct().getName() + "</span>");
//        }
//
//        if(placeViewBean.getPlaceProductList()!=null && !placeViewBean.getPlaceProductList().isEmpty())
//                placeViewBean.setProductListIdString(placeViewBean.getProductListIdString().substring(0, placeViewBean.getProductListIdString().length()-1));

        return "edit";
    }

    /**
     * クエリを実施して、展示場データを編集する
     */
    public String editPlace() {
        if (placeViewBean.getPlaceName().trim().equals("") || placeViewBean.getPlaceName() == null) {
            BeanUtil.setMessage("error", "展示場名を入力してください。");
            return null;
        }
        try {
            //入力したデータから展示場を再作成
            Place place = this.createPlaceFromAttibutes("update");

        //クエリを実施する　→　placeテーブル
            //getPlaceService().updatePlace(place);
            placeFacade.edit(place);

        ////クエリを実施する　→　place_product　テーブル
            //updatePlaceProductList();
        } catch (Exception e) {
            e.printStackTrace();
            BeanUtil.setMessage("error", "システムエラー：展示場を編集できない。");
            return null;
        }

		// 展示場リストをリフレッシュ
//    	this.search();
        //展示場一覧ページへ戻る。
        BeanUtil.setMessage("info", "展示場が編集されました。");
        return "index";
    }

    /**
     * query を実施して、展示場を削除する
     */
    public void deletePlace() {
//    	try{
//                //PlaceProduct　テーブルから削除
//    		//getPlaceService().deletePlaceByPrimaryKey(this.selectedPlace.getId());
//                placeService.deletePlaceProductByPlaceKey(placeViewBean.getSelectedPlace().getId());
//                
//    		//Place テーブルから削除
//    		placeService.deletePlaceByPrimaryKey(placeViewBean.getSelectedPlace().getId());
//
//    		
//    	}catch(Exception e){
//    		e.printStackTrace();
//    		BeanUtil.setMessage("error", BeanUtil.getMessageString("placeDeleteError"));
//    	}
//    	BeanUtil.setMessage("info", BeanUtil.getMessageString("placeDeleteSuccess"));
//        this.search();
        //return "index.jsf?faces-redirect=true";
    }

    /**
     * 選択された展示場からBeanの値を設定する
     *
     * @param place
     */
    public void fillAttributesFromPlace(Place place) {
        placeViewBean.setPlaceId(place.getId());
        placeViewBean.setPlaceName(place.getName());
        placeViewBean.setPlaceDelFlg(place.getDeleteFlg() == 1);
        placeViewBean.setPlacePrefectureId(place.getPrefecture().getId());
        placeViewBean.setPlaceMemo(place.getMemo());
    }

    /**
     * 入力されたデータを新規展示場を作成
     */
    public Place createPlaceFromAttibutes(String action) {
        Place place = new Place();
//           Aken aken = new Aken();
//           aken.setId(placeViewBean.getPlacePrefectureId());

        place.setName(placeViewBean.getPlaceName().trim());
        place.setPrefectureId(placeViewBean.getPlacePrefectureId());
        place.setDeleteFlg(placeViewBean.isPlaceDelFlg() ? 1 : 0);
        //place.setPrefecture(aken);
        place.setMemo(placeViewBean.getPlaceMemo());

        if (action.equals("update")) {
            place.setId(placeViewBean.getPlaceId());
            place.setUpdate(new Date());
            place.setDate(placeViewBean.getSelectedPlace().getDate());
        }
        if (action.equals("create")) {
            place.setDate(new Date());
        }

        return place;
    }

}
